import numpy as np
uconv = [30, 130., -70.]
kmax = 30.
def signal(x, t):
f = 0.
nwave = 50
for u in uconv:
for a, k, x0 in zip(np.random.rand(nwave), kmax*np.random.rand(nwave), np.random.rand(nwave)):
f += a*np.sin(2*np.pi*k*(x-x0-u*t))
return f
nt = 2048
nx = 512
ttot = .04
x = np.linspace(0., 1., nx)
t = np.linspace(0., ttot, nt)
xx, tt = np.meshgrid(x,t)
sig = signal(xx, tt)
#
import matplotlib.pyplot as plt
plt.rc('animation', html='jshtml')
import matplotlib.animation as pltanim
fig, ax = plt.subplots(1,1, figsize=(10,4))
line, = ax.plot(x, sig[0,:])
skip = 8 ; anim = pltanim.FuncAnimation(fig, lambda it: line.set_ydata(sig[skip*it,:]), frames=nt//skip, interval=50, repeat=True)
# skip = 8 ; anim = pltanim.FuncAnimation(fig, lambda it: ax.plot(x, sig[skip*it,:]), frames=nt//skip, interval=20, repeat=True)
anim